package com.ruoyi.alert.service.impl;

import com.ruoyi.alert.domain.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class AlertServiceImpl {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${alarm.redis-key-prefix}")
    private String redisKeyPrefix;

    @Value("${alarm.ttl-hours}")
    private int ttlHours;

    public boolean isDuplicate(Alert msg) {
        if (msg.getProjectId() == null || msg.getDate() == null) {
            throw new IllegalArgumentException("ProjectId 或 Date 不能为 null");
        }

        String key = redisKeyPrefix + msg.getProjectId() + ":" + msg.getDate().getTime();
        System.out.println("生成的 Redis Key: " + key);
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    public void markAsSent(Alert msg) {
        String key = redisKeyPrefix + msg.getProjectId() + ":" + msg.getDate().getTime();
        redisTemplate.opsForValue().set(key, "1", ttlHours, TimeUnit.HOURS);
    }
}